# -*- coding: utf-8 -*-

"""
@author: 陈列旺
@contact: QQ:55059
@Created on: 2021/9/13 
@Remark:
"""
import uuid

from django.db import models
from application import settings
from dvadmin.utils.models import CoreModel, table_prefix
from dvadmin.system.models import Dept

STATUS_CHOICES = (
    ('0', '末启动'),
    ('1', '运行中'), 
    ('2', '一级告警中'),
    ('3', '二级告警中'),
    ('4', '三级告警中'),
    ('5','已完成')
)
# cpt
# 0未开始，1进行中，2结束，9不报警，3一级，4二级，5三级

class PmsPrj(CoreModel):
    '''
    项目名称
    '''
    prjname = models.CharField(max_length=200,verbose_name="名称", help_text="项目名称")
    prj_stname = models.CharField(max_length=100,null=True,blank=True,verbose_name="简称",help_text="项目简称")
    prj_content = models.TextField(verbose_name="项目内容", null=True, blank=True, help_text="项目内容")
    prj_amount = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True,verbose_name="金额", help_text="项目总金额")
    prj_leader = models.CharField(max_length=14, blank=True, null=True,verbose_name="负责人", help_text="项目负责人")
    #phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
    #phone_number = models.CharField(validators=[phone_regex], max_length=17, blank=True) # validators should be a list
    prj_phone = models.CharField(max_length=11, blank=True, null=True,verbose_name="手机", help_text="移动电话")
    start_time = models.DateField(blank=True, null=True,verbose_name="开始时间", help_text="项目开始时间")
    end_time = models.DateField(blank=True, null=True,verbose_name="结束时间", help_text="项目结束时间")
    prj_status = models.SmallIntegerField(choices=STATUS_CHOICES, default=0,blank=True, null=True,verbose_name="状态", help_text="项目状态")
    operate = models.CharField(max_length=255, blank=True, null=True,verbose_name="其他信息", help_text="其他信息扩展用")
    # 下面两个计算字段可能更好,还有工日
    cpt_count = models.SmallIntegerField(blank=True, null=True,verbose_name="阶段数", help_text="项目总阶段数")
    cpt_on = models.SmallIntegerField(blank=True, null=True,verbose_name="所在阶段", help_text="项目当前所在阶段")
    place = models.SmallIntegerField(blank=True, null=True,verbose_name=u"进度", help_text="项目进度")
    risk = models.CharField(max_length=4, verbose_name=u'风险级别')
    wbs = models.CharField(max_length=50,blank=True, null=True, verbose_name=u'wbs',help_text="内部编码wbs")
     # 在普通一多一、一对多、多对多时，to='App名.模块名' 进行关联
    dept = models.ForeignKey(Dept, on_delete=models.PROTECT, verbose_name="项目所属部门", related_name='project_dept',
                      db_constraint=False)

    class Meta:
        #managed = False
        db_table = settings.TABLE_PREFIX +'pms_prj'
        verbose_name = '项目管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.prjname}"

class PmsCpt(CoreModel):
    '''
    任务里程表
    '''
    cpt_serial = models.SmallIntegerField(verbose_name='顺序',help_text='顺序',default=0)
    cptname = models.CharField(max_length=255,verbose_name=u'任务里程',help_text="任务里程名称")
    begin_time = models.DateTimeField(verbose_name="开始时间",null=False,blank=False,help_text='里程点开始时间,不能为空')    
    plan_time = models.DateTimeField(verbose_name="要求结束时间",null=True,blank=True,help_text='请输入里程点计划完成时间,不能为空')
    end_time = models.DateTimeField(verbose_name="实际结束时间",null=True,blank=True,help_text='里程点实际结束时间')
    #working_days= plan_time-begin_time ,程序中以工日作计算超时
    work_days =models.IntegerField(verbose_name="任务工日",null=False,blank=False,help_text="任务工日")
    cpt_content = models.TextField(blank=True, null=True,verbose_name="任务里程内容",help_text="任务里程内容")
    progress = models.IntegerField(verbose_name='权重值',blank=True,help_text=r'请输入任务进度权重计算值')
    #这里要事件机制,当前cpt_status变化后,通知prj的status变更
    cpt_status = models.SmallIntegerField(choices=STATUS_CHOICES, default=0,blank=True, null=True,verbose_name="状态", help_text="任务状态")
    operate = models.CharField(max_length=255, blank=True, null=True,verbose_name='扩展用')
    dept = models.ForeignKey(Dept,db_column='dept_id',null=True, blank=True, verbose_name="所属部门",on_delete=models.CASCADE)
    prj =models.ForeignKey(to='PmsPrj', verbose_name='所属项目', on_delete=models.CASCADE, db_constraint=False, null=True,
                             related_name='prj_cpts',blank=True, help_text="所属项目")
    person = models.ForeignKey(to=settings.AUTH_USER_MODEL, null=True, blank=True,verbose_name='负责人', related_name='person_cpts',
                         on_delete=models.SET_NULL, db_constraint=False)

    class Meta:
        #managed = False
        db_table = settings.TABLE_PREFIX +'pms_cpt'
        verbose_name = '任务里程表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.cptname


class PmsAtt(CoreModel):
    '''
    任务附件
    '''
    att = models.CharField(max_length=50, null=True, blank=True, verbose_name="附件", help_text="附件名称")
    url = models.FileField(upload_to='media/files/%Y%m%d/')
    cpt = models.ForeignKey(to='PmsCpt', verbose_name='所属里程', on_delete=models.CASCADE, db_constraint=False, null=True,
                             blank=True, help_text="关联里程")

    class Meta:
        #managed = False
        db_table = settings.TABLE_PREFIX + "pms_att"
        verbose_name = '附件表'
        verbose_name_plural = verbose_name
        ordering = ('-create_datetime',)

    def __str__(self):
        return f"{self.att}"


class PmsSlog(CoreModel):
    '''
    短信发送记录
    '''
    the_level = models.SmallIntegerField(verbose_name='告警级别',help_text='告警级别')
    send_time = models.DateField()
    phone = models.SmallIntegerField()
    cpt = models.ForeignKey(to='PmsCpt', verbose_name='所属里程', on_delete=models.CASCADE, db_constraint=False, null=True,
                             blank=True, help_text="关联里程")
    slog_status = models.SmallIntegerField()

    class Meta:
        #managed = False
        db_table = settings.TABLE_PREFIX + "pms_slog"